#include "BaseDrawable.h"

//=====STRUCTORS
BaseDrawable::BaseDrawable(int Obj, D3DXVECTOR3 V)
	:meiObjIndex(Obj), 
	mvPosition(V), 
	mvColor(cNeutral),
	Subset(0),
	meiDirIndex(0)
{
	Visible = true;
	MovedThisTurn = false;
	RotatedThisTurn = false;
}
//=====MUTATORS
bool  BaseDrawable::SetOwner(int i)
{
	if(i > Player2 || i < Neutral)
		return false;
	miOwner = i;

	RefreshColors();
	return true;
}
void  BaseDrawable::SetVisibility(bool V)
{
	Visible = V;
}
void  BaseDrawable::RefreshColors()
{
	(miOwner == Neutral? mvColor = cNeutral: NULL);
	(miOwner == Player1? mvColor = cP1Color: NULL);
	(miOwner == Player2? mvColor = cP2Color: NULL);
}
//=====POSITION
void BaseDrawable::SetPiece()
{
	mvsPosition = mvPosition;
	mvsRotation = mvRotation;
};
void BaseDrawable::ResetPiece()
{
	mvRotation = mvsRotation;
	mvPosition = mvsPosition;
};
void BaseDrawable::Move(int Direction){

	if(MovedThisTurn == true){
		mvPosition = mvsPosition;
		MovedThisTurn = false;
	}
	else{
		switch(Direction)
		{
		case DIR_NORTH:	mvPosition.z++;	break;
		case DIR_SOUTH:	mvPosition.z--; break;
		case DIR_WEST:	mvPosition.x--;	break;	
		case DIR_EAST:	mvPosition.x++;	break;
		}

		MovedThisTurn = true;
	}

};
//=====ROTATION
void  BaseDrawable::InitialRotate(int D)
{
	int TempDirection = D;
	meiDirIndex = DIR_NORTH;

	RotateClockWise();					//Turn once to activate proper rotation
	while(meiDirIndex!= TempDirection)	//Then turn everything until it fits.
		RotateClockWise();

}
float BaseDrawable::DegToRad(float Deg)
{
	return Deg*3.14f/180.0f;
}
void  BaseDrawable::RotateCClockwise()
{
	if(MovedThisTurn == true){
		mvRotation = mvsRotation;
		RotatedThisTurn = false;
	}
	else{
		(meiDirIndex == DIR_NORTH? meiDirIndex = DIR_WEST: meiDirIndex--);
		UpdateRotation(false);
		RotatedThisTurn = true;
	}
}
void  BaseDrawable::RotateClockWise()
{
	if(MovedThisTurn == true){
		mvRotation = mvsRotation;
		RotatedThisTurn = false;
	}
	else{
		(meiDirIndex == DIR_WEST? meiDirIndex = DIR_NORTH: meiDirIndex++);
		UpdateRotation(true);
		RotatedThisTurn = true;
	}
}
void  BaseDrawable::UpdateRotation(bool Clockwise)
{
	float XAngle;

	switch(meiDirIndex){
	case DIR_NORTH:	XAngle = 0.0f   * (3.14159f/180.0f); break;
	case DIR_EAST:	XAngle = 90.0f  * (3.14159f/180.0f); break;
	case DIR_SOUTH: XAngle = 180.0f * (3.14159f/180.0f); break;
	case DIR_WEST:	XAngle = 270.0f * (3.14159f/180.0f); break;
	default:											 break;
	}

	mvRotation = D3DXVECTOR3(XAngle, 0.0f, 0.0f);
}
//=====ACCESSORS
int   BaseDrawable::GetID()		{return meiObjIndex ;};
float BaseDrawable::GetX()		{return mvPosition.x;};
float BaseDrawable::GetY()		{return mvPosition.y;};
float BaseDrawable::GetZ()		{return mvPosition.z;};
